/**
 * 对mars3d的二次封装
 * author: wangyushan
 * date: 2021-05-18
 * 依赖mars3d
 */

function ThreeDFMap () {
  //对象合并
  var _extend = function (opt1, opt2){
    var opt1 = opt1 || {};
    if(opt2 && typeof opt2 == 'object'){
      for (var key in opt2) {
        opt1[key] = opt2[key]
      }
    }
    return opt1;
  }
  var viewerTemp = function (id, options) {
    var opts = {
      animation: false,        //是否创建动画小器件，左下角仪表
      timeline: false,         //是否显示时间线控件
      geocoder: false,         //是否显示地名查找控件
      sceneModePicker: true,  //是否显示投影方式控件
      homeButton: true,        //回到默认视域按钮
      baseLayerPicker: true,
    }
    var opts2 = _extend(opts, options)
    return new Cesium.Viewer(id,opts2)
  }
  //UI绑定数据 处理
  function initParamView() {
    var viewModel = {
      particleSize: 20.0,
      startScale: 1.0,
      endScale: 3.0,
      emissionRate: 200,
      minimumParticleLife: 1.2,
      maximumParticleLife: 3.2,
      minimumSpeed: 1.0,
      maximumSpeed: 4.0,
      gravity: -11.0,
    };

    Cesium.knockout.track(viewModel);
    // var toolbar = document.getElementById("toolbar");
    // Cesium.knockout.applyBindings(viewModel, toolbar);


    //绑定事件
    Cesium.knockout
      .getObservable(viewModel, "emissionRate")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.emissionRate = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "minimumParticleLife")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.minimumParticleLife = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "maximumParticleLife")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.maximumParticleLife = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "minimumSpeed")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.minimumSpeed = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "maximumSpeed")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.maximumSpeed = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "startScale")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.startScale = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "endScale")
      .subscribe(function (newValue) {
        particleSystemEx.particleSystem.endScale = parseFloat(newValue);
      });

    Cesium.knockout
      .getObservable(viewModel, "particleSize")
      .subscribe(function (newValue) {
        var particleSize = parseFloat(newValue);
        particleSystemEx.particleSize = particleSize;
      });

    Cesium.knockout
      .getObservable(viewModel, "gravity")
      .subscribe(function (newValue) {
        var gravity = parseFloat(newValue);
        particleSystemEx.gravity = gravity;
      });

  }
  return {
    //创建地图
    createMap(id, opt1, opt2){
      var viewer = viewerTemp(id, opt1);
      var opt2 = opt2 || {};
      var oriOpt =  {
        contextmenu: true,
        center: { "y": 20.758452, "x": 108.198353, "z": 7733735.96, "heading": 357.7, "pitch": -82.4, "roll": 0.9 },
        mouseZoom: true,
        location: {
          format: "<div>经度:{x}</div> <div>纬度:{y}</div> <div>海拔：{z}米</div> <div>方向：{heading}度</div> <div>视高：{height}米</div>"
        },
        navigation: {
          compass: { top: "10px", right: "5px" }
        },
      }
      var newOpt = _extend(oriOpt, opt2)
      viewer.mars = new mars3d.ViewerEx(viewer, newOpt);
      return viewer;
    },
    /**
     * 添加图片
     * @param viewer
     * @param options
     */
    addImageLayer(viewer, options){
      var opt = {
        "type": "image",
        "url": "",
      }
      var newOpt = _extend(opt, options);
      var imageryProvider = mars3d.layer.createImageryProvider(newOpt);
      var imagelayer = new Cesium.ImageryLayer(imageryProvider, { alpha: 1 })
      viewer.imageryLayers.add(imagelayer);
    },
    /**
     * 添加点
     * @param viewer
     * @param options
     * @return {Entity}
     */
    addPoint(viewer, options){
      var opt = {
        name: "点",
        position: Cesium.Cartesian3.fromDegrees(116.308659, 30.914005, 429.94),
        point: {
          color: new Cesium.Color.fromCssColorString("#ff0000"),
          pixelSize: 10,
          outlineColor: new Cesium.Color.fromCssColorString("#ffffff"),
          outlineWidth: 2,
        }
      }
      var newOpt = _extend(opt, options);
      var entity = viewer.entities.add(newOpt);
      return entity;
    },
    /**
     * 添加动态点
     * @param viewer
     * @param opt
     * @param options
     * @return {*}
     */
    addAnimationPoint(viewer, opt, options){
      var newOpt1 = {
        color: opt.color || '',
        scale: opt.scale || 1,
      }

      var html = '<div class="mars3d-animation-point" style="color:' + newOpt1.color + ';transform: scale('+ newOpt1.scale + ');"><p></p></div>';
      var oriOpt = {
        html: html,
        position: Cesium.Cartesian3.fromDegrees(118.810112, 31.231319),
        anchor: [0, 0],
        data: { }, //item为需要绑定的数据
        click: function (e) {//单击后的回调

        }
      }
      var newOpt2 = _extend(oriOpt, options);
      var divPoint = new mars3d.DivPoint(viewer, newOpt2);
      return divPoint;
    },
    /**
     * 添加粒子系统
     * @param viewer
     * @param options
     */
    addParticleSystem(viewer, options){
      var opt =  {
        //ParticleSystem原生属性
        image: './img/particle/smoke.png',
        startColor: Cesium.Color.LIGHTCYAN.withAlpha(0.3),    //粒子出生时的颜色
        endColor: Cesium.Color.WHITE.withAlpha(0.0),   //当粒子死亡时的颜色
        particleSize: 20,  //粒子图片的Size大小（单位：像素）
        startScale: 2.0, //粒子在出生时的比例（单位：相对于imageSize大小的倍数）
        endScale: 4.0,   //粒子在死亡时的比例（单位：相对于imageSize大小的倍数）
        minimumParticleLife: 1.0,//粒子可能存在的最短寿命时间，实际寿命将随机生成（单位：秒）
        maximumParticleLife: 3.0,//粒子可能存在的最长寿命时间，实际寿命将随机生成（单位：秒）
        emissionRate: 200, //粒子发射器的发射速率 （单位：次/秒）
        //mars扩展的属性
        position: Cesium.Cartesian3.fromDegrees(117.221844, 31.813857, 28.5), //位置
        gravity: -11,  //重力因子，会修改速度矢量以改变方向或速度（基于物理的效果）
        target: new Cesium.Cartesian3(-0.151, 0.294, 0.225), // 粒子的方向
        maxHeight: 5000,  //超出该高度后不显示粒子效果
      }
      var newOpt = _extend(opt, options);
      var particleSystemEx = new mars3d.ParticleSystemEx(viewer,newOpt);

      return particleSystemEx;
    }
  }
}

export default ThreeDFMap();
